import Vue from 'vue'
import Vuex from 'vuex'

import {
  sysJournalQuery,
  sysJournalDelete,
  sysJournalAdd,
  sysJournalUpdate,
  sysJournalOpenApiQuery,
  sysJournalDeleteBatch,
  sysJournalImport,
} from '@/api/module/sysJournal'

Vue.use(Vuex)

const state = {
  journalFlag: true, //true:列表页 false:新增页面
  curJournal: {},
  homeJournal: {},
}

const getters = {
  journalFlag: (state) => state.journalFlag,
  curJournal: (state) => state.curJournal,
  homeJournal: (state) => state.homeJournal,
}

const actions = {
  async querySysJournal({ commit }, listQuery) {
    let res = await sysJournalQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteSysJournal({ commit }, row) {
    let res = await sysJournalDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteBatchSysJournal({ commit }, data) {
    let res = await sysJournalDeleteBatch(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async addSysJournal({ commit }, data) {
    let res = await sysJournalAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateSysJournal({ commit }, data) {
    let res = await sysJournalUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async queryOpenApiSysJournal({ commit }, listQuery) {
    let res = await sysJournalOpenApiQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async importSysJournal({ commit }, data) {
    let res = await sysJournalImport(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  changeJournalFlag: (state, d) => (state.journalFlag = d),
  setCurJournal: (state, d) => (state.curJournal = d),
  setHomeJournal: (state, d) => (state.homeJournal = d),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
